/*
 *  Copyright 2008 by Spectrum Digital Incorporated.
 *  All rights reserved. Property of Spectrum Digital Incorporated.
 */

/*
 *  RTC implementation
 *
 */
#include "C6747_rtc.h"
#include "C6747.h"
#include "C6747_i2c.h"

/* ------------------------------------------------------------------------ *
 *                                                                          *
 *  _RTC_init( )                                                            *
 *                                                                          *
 *      Initialize RTC                                                      *
 *                                                                          *
 * ------------------------------------------------------------------------ */
C6747_time starttime;
C6747_time newtime = { // Values of time to be set
    0x08,                 // Calender value for seconds
    0x31,                 // Calender value for minutes
    0x17,                 // Calender value for hours
    0x11,                 // Calender value for days 
    0x02,                 // Calender value for months 
    0x09,                 // Calender value for years
    0x03,                 // Calender value for weeks
};


Int16 C6747_RTC_getTime(C6747_time *newtime)
{

	Uint8 data1[2] = {0x00,0x30};
	Uint8 data2[8];

	C6747_I2C_write( 0x6F , data1 , 2 );//access to the RTC,not the EEPROM
//	C6747_waitusec( 1000 );
	C6747_I2C_read( 0x6F , data2 , 7 ) ;
	C6747_waitusec( 1000 );

//    while (RTC_STAT & 0x01);    // Check for RTC busy bit
    newtime -> ss = data2[0];                
	newtime -> mm = data2[1];
    newtime -> hh = data2[2];
    newtime -> dd = data2[3];
    newtime -> mn = data2[4];
    newtime -> yy = data2[5];
	newtime -> ww = data2[6];

    return 0;
}

Int16 C6747_RTC_setTime(C6747_time *newtime)
{
	Uint8 datass[3] = { 0x00 , 0x30 , 0x01 };
	Uint8 datamm[3] = { 0x00 , 0x31 , 0x01 };
	Uint8 datahh[3] = { 0x00 , 0x32 , 0x01 };
	Uint8 datadd[3] = { 0x00 , 0x33 , 0x01 };
    Uint8 datamn[3] = { 0x00 , 0x34 , 0x01 };
	Uint8 datayy[3] = { 0x00 , 0x35 , 0x01 };
	Uint8 dataww[3] = { 0x00 , 0x36 , 0x01 };

	Uint8 data3[3] = { 0x00 , 0x3F , 0x02 }; 
	Uint8 data4[3] = { 0x00 , 0x3F , 0x06 };

	datass[2] = newtime->ss ;
	datamm[2] = newtime->mm ;
	datahh[2] = newtime->hh ;
	datadd[2] = newtime->dd ;
	datamn[2] = newtime->mn ;	
	datayy[2] = newtime->yy ;	
	dataww[2] = newtime->ww ;
		            


	C6747_I2C_write( 0x6F , data3 , 3 );
	C6747_I2C_write( 0x6F , data4 , 3 );
	C6747_I2C_write( 0x6F , datass , 3 );


	C6747_I2C_write( 0x6F , data3 , 3 );
	C6747_I2C_write( 0x6F , data4 , 3 );
	C6747_I2C_write( 0x6F , datamm , 3 );


	C6747_I2C_write( 0x6F , data3 , 3 );
	C6747_I2C_write( 0x6F , data4 , 3 );
	C6747_I2C_write( 0x6F , datahh , 3 );


	C6747_I2C_write( 0x6F , data3 , 3 );
	C6747_I2C_write( 0x6F , data4 , 3 );
	C6747_I2C_write( 0x6F , datadd , 3 );


	C6747_I2C_write( 0x6F , data3 , 3 );
	C6747_I2C_write( 0x6F , data4 , 3 );
	C6747_I2C_write( 0x6F , datamn , 3 );


	C6747_I2C_write( 0x6F , data3 , 3 );
	C6747_I2C_write( 0x6F , data4 , 3 );
	C6747_I2C_write( 0x6F , datayy , 3 );


	C6747_I2C_write( 0x6F , data3 , 3 );
	C6747_I2C_write( 0x6F , data4 , 3 );
	C6747_I2C_write( 0x6F , dataww , 3 );


    return 0;
}

void I2C_GPIO( )

{

	PINMUX8 = 0x00022000;//select the IIc CLK and DAT

}

